<template>
  <view>
    <view class="search-box">
      <!-- focus用于设置自动聚焦 -->
      <!-- 在uni-search-bar设置style只对h5有效 -->
      <uni-search-bar 
        :focus="true" 
        style="background-color: #c50000;" 
        placeholder="请输入搜索内容" 
        :radius="100" 
        cancelButton="none"
        @input="input"
      >
      </uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="suggest-list" v-if="keyword.length !== 0">
      <view 
        class="suggest-item" 
        v-for="(item, index) in searchResultsList" 
        :key="index" 
        @click="gotoDetail(item)"
      >
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="cleanHistoryList"></uni-icons>
      </view>
      <!-- 列表区 -->
      <view class="history-list">
        <uni-tag 
          :text="item" 
          v-for="(item, index) in histories" @click="gotoGoodsList(item)"
          :key="index"
        ></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null, // 设置定时器，用于防抖
        keyword: "",
        searchResultsList: [], // 搜索结果列表
        historyList: [], // 搜索历史列表
      };
    },
    onLoad(){
      this.historyList = JSON.parse(uni.getStorageSync('keyword') || "[]")
    },
    computed: {
      histories(){
        return [...this.historyList].reverse();
      }
    },
    methods:{
      input(searchContent){
        clearTimeout(this.timer); // 500ms内有输入就取消定时器
        this.timer = setTimeout(()=>{
          this.keyword = searchContent;
          this.getSearchList();
          // console.log(searchContent)
        }, 500);
      },
      async getSearchList(){
        if(this.keyword === ""){
          this.searchResultsList = [];
          return;
        }
        const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.keyword});
        if(res.meta.status !== 200) return uni.$showMsg();
        this.searchResultsList = res.message;
        this.saveSearchHistory();
      },
      gotoDetail(item){
        uni.navigateTo({
          url: `/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
        })
      },
      saveSearchHistory(){
        const set = new Set(this.historyList);
        set.delete(this.keyword); // 将重复的关键词删除以便其放到最新的位置
        set.add(this.keyword);
        this.historyList = Array.from(set);
        // 对搜索历史记录进行持久化的存储
        uni.setStorageSync('keyword', JSON.stringify(this.historyList));
      },
      cleanHistoryList(){
        this.historyList = [];
        uni.setStorageSync("keyword", "[]");
      },
      gotoGoodsList(keyword){
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?query=${keyword}`
        })
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky; // 吸顶
  top: 0;
  z-index: 999; // 防止被覆盖
  background-color: #c00000;
}
.suggest-list{
  padding: 0 10rpx;
  .suggest-item{
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 24rpx;
    padding: 26rpx 0;
    border-bottom: 1rpx solid #efefef;
    .goods-name{
      white-space: nowrap; // 不允许换行
      overflow: hidden; // 隐藏文本溢出的部分
      text-overflow: ellipsis; // 用小圆点代替溢出的部分
    }
  }
}
.history-box{
  padding: 0 10rpx;
  .history-title{
    display: flex;
    justify-content: space-between;
    height: 80rpx;
    align-items: center;
    font-size: 26rpx;
    border-bottom: 1rpx solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    margin-top: 5rpx;
    .uni-tag{
      margin-top: 10rpx;
      margin-right: 10rpx;
      background-color: #f0f0f0;
      color: #000000;
      border: none;
    }
  }
}

</style>
